<?php

namespace App\Services\Machine;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * 设备出货类
 */
class OutGoods
{
	/**
	 * [outGoods 出货]
	 * @param  [type]  $order_no   [订单编号]
	 * @param  [type]  $freight_no [货道编号]
	 * @param  [type]  $imei       [设备编号]
	 * 
	 */
	public function outGoods($order_no, $freight_no, $imei)
	{
		if($order_no && $freight_no && $imei){
			$machine = DB::table('machines')->where('imei',$imei)->first();
			$freight = DB::table('freight_ways')->where(['No' => $freight_no, 'machine_id' => $machine->id])->first();
			$order_info = DB::table('orders')->where('ordersn', $order_no)->first();
			if($order_info){
				$create_time = strtotime($order_info->created_at);
				if($order_info->order_status > 1 ||  $create_time + 300 < time()){
					return;
				}				
			}
			if($machine && $freight){
				$put_data['IMEI'] = (string)$imei;
	            $put_data['Command'] = 6;
	            $put_data['Slot_Index'] = (int)$freight_no;
	            $put_data['Number'] = 1;
	            $put_data['Order_Number'] = (string)$order_no;
	            $put_data['Count'] = 1;
	            $put_data['Type'] = (int)$freight->type;
	            $put_data['Time'] = time();
	            $data['order_no'] = $order_no;
	            $data['imei'] = $imei;
	            // $data['out_data'] = json_encode($put_data);
				// DB::table('out_goods')->insert($data);
				$data['out_data'] = $put_data;
				Redis::lpush('orders',json_encode($data));
				DB::table('mqtt_logs')->insert(['key'=>'插入出货队列', 'value' => json_encode($data), 'imei' => $imei, 'created_at' => date('Y-m-d H:i:s',time())]);
			}
		}
	}
}